#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>

//http://cloud.arest.io/name/fuction?params=0
WiFiClient espClient;
PubSubClient client(espClient);

// Create aREST instance
aREST rest = aREST(client);

char* device_id = "dd2co4";// Unique ID to identify the device for cloud.arest.io
const char* ssid = "your-wifi";// WiFi
const char* password = "your-wifi-password";

// Handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) 
{
  rest.handle_callback(client, topic, payload, length);
}

// 将此函数暴露给API
int ledControl(String command) 
{
  // Get state from command
  int state = command.toInt();
  digitalWrite(D5,state);
  return 1;
}

void setup(void)
{
  Serial.begin(115200);
  // Set callback
  client.setCallback(callback);
  rest.function("led",ledControl);// Function to be exposed
  rest.set_id(device_id);// Give name and ID to device
  rest.set_name("my_esp8266");

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  char* out_topic = rest.getBuffer();// Set output topic

}

void loop() 
{
  rest.loop(client);
}



